﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Text;

namespace SerialTool
{
    /// <summary>
    /// 学习字符串
    /// 每条记录信息包含名字和内容
    /// 记录文件保存路在启动文件夹的temp目录下的LrnS.txt文件
    /// </summary>
    static class LrnS
    {
        #region Inner Variables

        static private string LrnSPath=Application.StartupPath + @"\temp\LrnS.txt";
        static private string myM = "@";

        #endregion

        /// <summary>
        /// 通过name获取其对应的内容
        /// </summary>
        /// <param name="name">该保存项的名字</param>
        /// <returns></returns>
        static public string GetItem(string name)
        {
            ArrayList myItems = ReadFile();
            string head = name + myM;
            string message;
            for (int i = 0; i < myItems.Count; i++)
            {
                message = (string)myItems[i];
                if (message.StartsWith(head))
                {
                    message = message.Substring(head.Length);
                    return message;
                }
            }
            return "";
        }

        /// <summary>
        /// 获取所有符合记录的name
        /// </summary>
        /// <param name="grep">条件</param>
        /// <param name="message">信息</param>
        static public string[] GetAllItemNameGrep(string[] grep)
        {
            ArrayList allItems = ReadFile();
            string[] allNames = new string[allItems.Count];

            for (int i = 0; i < allItems.Count; i++)
            {
                allNames[i] = (string)allItems[i];
                if (allNames[i].Contains(myM))
                {
                    int count = allNames[i].IndexOf(myM);
                    allNames[i] = allNames[i].Substring(0, count);
                }
            }
            ArrayList itemsF = new ArrayList();

            for (int i = 0; i < allNames.Length; i++)
            {
                string theiiiit = allNames[i];

                if (grep.Length == 0)
                {
                    itemsF.Add(theiiiit);
                }
                else
                {
                    for (int j = 0; j < grep.Length; j++)
                    {
                        if (!theiiiit.Contains(grep[j]))
                            break;
                        if (j == grep.Length - 1)
                        {
                            itemsF.Add(theiiiit);
                        }
                    }
                }
            }

            string[] arrstr = new string[itemsF.Count];
            for (int i = 0; i < itemsF.Count; i++)
            {
                arrstr[i] = (string)itemsF[i];
            }
            return arrstr;
        }

        /// <summary>
        /// 把字符串信息加名字保存
        /// </summary>
        /// <param name="name">项名称</param>
        /// <param name="message">信息</param>
        static public void SaveItem(string name, string message)
        {
            ArrayList myRecs = ReadFile(); ;
            string dir = Path.GetDirectoryName(LrnSPath);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            using (FileStream fs = new FileStream(LrnSPath, FileMode.Create))
            {
                TextWriter wt = new StreamWriter(fs, Encoding.Default);
                fs.Seek(0, SeekOrigin.End);

                addItem(name, message, myRecs);

                for (int i = 0; i < myRecs.Count; i++)
                {
                    wt.WriteLine((string)myRecs[i]);
                }

                wt.Close();
                fs.Close();
            }
        }


        /// <summary>
        /// 私有函数
        /// 读取缓存文件
        /// </summary>
        static private ArrayList ReadFile()
        {
            ArrayList myRecs;
            myRecs = new ArrayList();
            if (!File.Exists(LrnSPath))
                return myRecs;
            using (StreamReader fs = new StreamReader(LrnSPath, Encoding.Default))
            {
                string str = "";
                while (true)
                {
                    str = fs.ReadLine();
                    if (str == null)
                        break;
                    str = str.Trim();
                    if (str.Contains(myM))
                        myRecs.Add(str);
                }
                fs.Dispose();
                fs.Close();
            }
            return myRecs;
        }


        /// <summary>
        /// 私有函数
        /// 在记录链表中更新/增加记录，删除多余的同名记录
        /// </summary>
        /// <param name="name">路径名称</param>
        /// <param name="fp">路径信息</param>
        static private void addItem(string name, string fp, ArrayList myRecs)
        {
            string rec;
            for (int i = 0; i < myRecs.Count; i++)
            {
                rec = (string)myRecs[i];
                if (rec.StartsWith(name + myM))
                {
                    myRecs.RemoveAt(i);
                    i--;
                }
            }

            myRecs.Insert(0, name + myM + fp);
        }


        //*******************************************************************************************
    }
}
